package com.eastfair.common.service.impl;

import cn.hutool.core.date.StopWatch;
import com.eastfair.common.dto.GmApiPrjAndHostRelDtos;
import com.eastfair.common.dto.GmApiPrjAndSvcRelDtos;
import com.eastfair.common.dto.GmApiPrjInfDtos;
import com.eastfair.common.res.CrmResult;
import com.eastfair.common.service.CommonInterfaceCallRecordService;
import com.eastfair.common.service.CrmPrjInfSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Crm 同步项目信息 service.
 *
 * @author dqq
 * @date 2023 /2/13
 */
@Service
@Slf4j
public class CrmPrjInfSyncServiceImpl implements CrmPrjInfSyncService {

    @Value("${roma.ipport}")
    private String romaIpport;

    @Value("${roma.CRM-X-HW-ID}")
    private String romaXHWID;

    @Value("${roma.CRM-X-HW-APPKEY}")
    private String romaXHWAPPKEY;

    @Value("${crm.customer_object.url}")
    private String crmCustomerObjectUrl;

    @Value("${crm.customer_object_sponsor.url}")
    private String crmCustomerObjectSponsorUrl;

    @Value("${crm.customer_service_provider.url}")
    private String crmCustomerServiceProviderUrl;

    @Resource
    private CommonInterfaceCallRecordService commonInterfaceCallRecordService;

    @Override
    public CrmResult syncPrjInf(GmApiPrjInfDtos gmApiPrjInfDtos) {
        log.info("syncPrjInf - 同步项目信息, gmApiPrjInfDtos={}", gmApiPrjInfDtos);
        String httpUrl = romaIpport + crmCustomerObjectUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncPrjInf");
        String result = commonInterfaceCallRecordService.processHttp(httpUrl, gmApiPrjInfDtos, romaXHWID, romaXHWAPPKEY);
        log.info("syncPrjInf - 同步项目信息结果, result={}", result);
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());
        return null;
    }

    @Override
    public CrmResult syncPrjAndHostRel(GmApiPrjAndHostRelDtos gmApiPrjAndHostRelDtos) {
        log.info("syncPrjAndHostRel - 同步项目与主办方关系, gmApiPrjAndHostRelDtos={}", gmApiPrjAndHostRelDtos);
        String httpUrl = romaIpport + crmCustomerObjectSponsorUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncPrjAndHostRel");
        String result = commonInterfaceCallRecordService.processHttp(httpUrl, gmApiPrjAndHostRelDtos, romaXHWID, romaXHWAPPKEY);
        log.info("syncPrjAndHostRel - 同步项目与主办方关系完成, result={}", result);
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());
        return null;
    }

    @Override
    public CrmResult syncPrjAndSvcRel(GmApiPrjAndSvcRelDtos gmApiPrjAndSvcRelDtos) {
        log.info("syncPrjAndSvcRel - 同步项目与服务商关系, gmApiPrjAndSvcRelDtos={}", gmApiPrjAndSvcRelDtos);
        String httpUrl = romaIpport + crmCustomerServiceProviderUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncPrjAndSvcRel");
        String result = commonInterfaceCallRecordService.processHttp(httpUrl, gmApiPrjAndSvcRelDtos, romaXHWID, romaXHWAPPKEY);
        log.info("syncPrjAndSvcRel - 同步项目与服务商关系结果, result={}", result);
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());
        return null;
    }
}